package com.aniu.portforwarder;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener{

    private EditText localPort;
    private EditText remoteHost;
    private EditText remotePort;

    public static final String LOCAL_PORT = "LOCAL_PORT";
    public static final String REMOTE_HOST = "REMOTE_HOST";
    public static final String REMOTE_PORT = "REMOTE_PORT";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.start).setOnClickListener(this);

        localPort = (EditText) findViewById(R.id.localPort);
        remoteHost = (EditText) findViewById(R.id.remoteHost);
        remotePort = (EditText) findViewById(R.id.remotePort);
    }

    @Override
    public void onClick(View view) {
        String slocalPort = localPort.getText().toString();
        String host = remoteHost.getText().toString();
        String port = remotePort.getText().toString();

        if(slocalPort == null || "".equals(slocalPort)){
            slocalPort = "12345";
        }

        if(host == null || "".equals(host)) {
            host = "sourceforge.net";
        }

        if(port == null || "".equals(port)) {
            port = "443";
        }

        if (view.getId() == R.id.start) {
            Intent intent = new Intent(this, ForwardService.class);
            intent.putExtra(LOCAL_PORT, slocalPort);
            intent.putExtra(REMOTE_HOST, host);
            intent.putExtra(REMOTE_PORT, port);

            startService(intent);
        }
    }
}